import os, os.path

join = os.path.join
dirname = os.path.dirname
# declare LEVIN vars
LEVINDIR,LEVIN_ICONS, LEVIN_DATA, LEVIN_PO, LEVIN_ICONS_NOPIC, LEVIN_WORLD = ("",)*6
def rebuild_LEVIN(**dic):
    global LEVINDIR,LEVIN_ICONS, LEVIN_DATA, LEVIN_PO, LEVIN_ICONS_NOPIC, LEVIN_WORLD
    LEVINDIR    = dic.get('LEVINDIR', dirname(__file__) )
    LEVINDIR_up = dirname(LEVINDIR)
    LEVIN_ICONS = dic.get('LEVIN_ICONS',  join( LEVINDIR_up, 'icons'))
    LEVIN_DATA  = dic.get('LEVIN_DATA',   join( LEVINDIR_up, 'data'))
    LEVIN_PO    = dic.get('LEVIN_PO',     join( LEVINDIR_up, 'po'))
    LEVIN_ICONS_NOPIC = dic.get('LEVIN_ICONS_NOPIC', join( LEVIN_ICONS, 'nopic.png'))
    LEVIN_WORLD =  dic.get('LEVIN_WORLD', join(LEVIN_DATA, 'world.pk') )

# define default LEVIN vars
rebuild_LEVIN()




USER_HOME = os.path.expanduser('~')


# declare USER vars
USER_DIR, USER_SAVE, USER_DATA_DIR , USER_CONFIG = ("",)*4

def rebuild_USER(**dic):
    global USER_DIR, USER_CONFIG, USER_SAVE, USER_DATA_DIR
    USER_DIR =        dic.get('USER_DIR',     join( os.environ['HOME'], '.config', 'LeVin' ) )
    USER_SAVE =       dic.get('USER_SAVE',    join( USER_DIR, 'user.pk'))
    USER_DATA_DIR =   dic.get('USER_DATA_DIR',join( USER_DIR, 'data') )
    USER_CONFIG =     dic.get('USER_CONFIG',  join( USER_DIR, 'config.ini') )

# define default USER vars
rebuild_USER()

DEBUG_MODE = False


application = None

def init():
    global USER_DIR
    if not os.path.exists( USER_DIR):
        os.mkdir( USER_DIR )
        
    

def dataPath( fname):
    return join( LEVIN_DATA, fname)

def iconPath( fname):
    return join( LEVIN_ICONS, fname)


from datetime import date as DateObj
import locale
locale.setlocale(locale.LC_ALL, '')
dateformat = locale.nl_langinfo(locale.D_FMT)
date_today = DateObj.today()



_dateFromOrd = DateObj.fromordinal
def dateFromOrdinal(d):
    return _dateFromOrd(d).strftime(dateformat)

import time
def ordinalFromDate(d):
    t = time.strptime(d,dateformat)
    return DateObj(year=t.tm_year, day=t.tm_mday, month=t.tm_mon).toordinal()

def buildUserDataDir():
    if not os.path.exists(USER_DATA_DIR):
        os.mkdir(USER_DATA_DIR)

        



def guessSynchroPath():
    print dirname(LEVINDIR)+'/testData'
    return dirname(LEVINDIR)+'/testData'
    #return LEVINDIR



def urlMaker(words):
    wo= sum(  [w.split(' ') for w in words if w ], [] )
    s='+'.join( wo )
    return 'http://www.google.com/?#q='+s
def webSearch(words):
    from os import system
    print 'searching ', urlMaker(words)
    system('xdg-open '+urlMaker(words))
    pass
def webSearchBottle(b):
    webSearch( [b.cru_name, b.productor])

# the main world infrastructure object.
wineWorld = None
